
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from deptSite.hrweb.models import Page, Employee, Unit, File

def render(template, payload):
    payload['pages_created'] = Page.objects.all().order_by('-created')
    payload['pages_updated'] = Page.objects.all().order_by('-updated')
    #payload['recent_tutorials'] = Tutorial.objects.all().order_by('-date_created')
    payload['units'] = Unit.objects.all()
    return render_to_response(template, payload)

def index(request,):
    employees = Employee.objects.all()
    return render('index.html',{'employee_list':employees})

def unit_index(request,unit_slug):
    unit = Unit.objects.get(slug=unit_slug)
    pages_list = Page.objects.filter(unit=unit.id)
    files_list = File.objects.filter(unit=unit.id)
    return render('unit.html',{'unit':unit,'pages_list':pages_list,'files_list':files_list})

def unit_page_list(request,unit_slug):
    unit = Unit.objects.get(slug=unit_slug)
    pages_list = Page.objects.filter(unit=unit.id)
    return render('unit_page_list.html',{'unit':unit,'pages_list':pages_list})

def unit_sub_page(request,unit_slug,page_slug):
    unit = Unit.objects.get(slug=unit_slug)
    page = Page.objects.get(slug=page_slug)
    pages_list = Page.objects.filter(unit=unit.id)
    return render('unit_page.html',{'unit':unit,'pages_list':pages_list,'page':page})

def contacts(request):
    employees = Employee.objects.all()
    return render('contacts.html',{'employee_list':employees})